<template>
  <div class="add-interface">
    <!--添加按钮-->
    <el-button type="primary" size='small' @click="clickAddInterface(InterfaceForm.projectId)"><i class="el-icon-plus"></i>添加接口</el-button>

    <!--添加接口弹框-->
    <el-dialog title="添加接口" :visible.sync="dialogVisible" :append-to-body="true" :close-on-click-modal="false" width="620px">

      <!--form 提交表单-->
      <el-form @submit.native.prevent :rules="interfaceRules" ref="InterfaceForm" :model="InterfaceForm">
        <!--接口分组-->
        <el-form-item label="接口分组:" label-width="100px" prop="apiGroupId" required>
          <el-select placeholder="请选择接口分组" style="width:100%" v-model="InterfaceForm.apiGroupId">
            <el-option v-for="item in groupList" :key="item.id" :label="item.label" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <!--接口名-->
        <el-form-item label="接口名:" label-width="100px" prop="name" required>
          <el-input placeholder="请输入接口名" v-model="InterfaceForm.name"></el-input>
        </el-form-item>
        <!--路径-->
        <el-form-item label="接口路径:" label-width="100px" prop="path" required>
          <el-input placeholder="请输入路径，以 ’/‘开头" class="input-with-select" v-model="InterfaceForm.path">
            <el-select v-model="InterfaceForm.method" slot="prepend" placeholder="请选择" style="width:90px">
              <el-option label="GET" value="GET"></el-option>
              <el-option label="POST" value="POST"></el-option>
              <el-option label="PUT" value="PUT"></el-option>
              <el-option label="DELETE" value="DELETE"></el-option>
            </el-select>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
        <el-button type="primary" @click="addApi(InterfaceForm)" size="medium">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getApiGroup } from '@/api/apiGroup'
import { addApi } from '@/api/api'
export default {

  data () {
    return {

      dialogVisible: false,

      groupList: [],

      //接口提交表单
      InterfaceForm: {
        projectId: this.$store.state.projectId,
        apiGroupId: "",
        method: "GET",
        status: 0,
        // params: [],
        // commonParams: [],
        // headers: [],
        // cookies: [],
        author: "aitesting",
        protocol: "http",
        path: "",
        requestType: 'form'
      },

      interfaceRules: {
        apiGroupId: [
          { required: true, message: '请选择接口分组', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入接口名', trigger: 'blur' },
        ],
        path: [
          { required: true, message: '请输入接口路径', trigger: 'blur' },
        ],
      },


    }
  },

  methods: {

    //点击添加按钮
    clickAddInterface (projectId) {
      this.dialogVisible = true
      this.groupList = []
      getApiGroup(projectId).then(resp => {
        if (resp.code == 200) {
          this.groupList = resp.data
        }
      })
    },

    //点击确定按钮触发，  添加接口功能
    addApi (InterfaceForm) {
      this.InterfaceForm.updateTime = new Date()
      this.InterfaceForm.createTime = new Date()

      this.$refs["InterfaceForm"].validate((valid) => {
        if (valid) {
          addApi(InterfaceForm).then(resp => {
            if (resp.code == 200) {
              this.dialogVisible = false
              this.$message({
                message: resp.message,
                type: 'success'
              });

              this.$emit("listenTochildEvent", this.InterfaceForm.projectId, this.$store.state.apiGroupId, '', 1, 30)
            } else {
              this.$message({
                message: resp.message,
                type: 'warning'
              });
            }


          })

        }


      })


    }
  },

  mounted () {
  }




}
</script>

<style scoped>
.add-interface {
  float: right;
  margin-top: 10px;
  margin-right: 4px;
  text-align: right;
}
</style>